package sourcePackage;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.util.Scanner;

public class Text_Entry extends JPanel implements ActionListener {
	protected JTextField textField;
    protected JTextArea textArea;
    protected JLabel label1;
    private final static String newline = "\n";
    
    public Text_Entry(String taskQuestion) throws SQLException {
        super(new GridBagLayout());
    	//label1 displays the assignment Task
        label1 = new JLabel(taskQuestion, JLabel.CENTER);
        //Set the position of its text, relative to its icon:
        label1.setVerticalTextPosition(JLabel.TOP);
        label1.setHorizontalTextPosition(JLabel.CENTER);
    	
        //textField is where the user enters their request
        textField = new JTextField(45);
        textField.addActionListener(this);

        textArea = new JTextArea(5, 45);
        textArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(textArea);

        //Add Components to this panel.
        GridBagConstraints c = new GridBagConstraints();
        c.gridwidth = GridBagConstraints.REMAINDER;

        c.fill = GridBagConstraints.HORIZONTAL;
        
        add(label1, c);
        add(textField, c);
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;
        c.weighty = 1.0;
        add(scrollPane, c);
	
    }

    public void actionPerformed(ActionEvent evt) {
    	
    	int taskNumber = Button_Menu.getTaskNumber();
    	//Input Checks, password is hashed?
    	textArea.append("Task Number: " + taskNumber + newline);
    	textArea.append(Log_In.getUser() + newline);
    	textArea.append(Log_In.getPassword().toString()+newline);
    	
    	//float that stores the value of support
    	float s = 0.0f;
    	//fetches the string in the textField
    	String text = textField.getText();
    	 try
    	 {
    		s = Float.valueOf(text.trim()).floatValue();
    	 }
    	 catch (NumberFormatException nfe)
    	 {
    		 text = ("NumberFormatException: " + nfe.getMessage());
    	 }
    	 
    	 if(taskNumber == 1)
    	 {
	    	 try
	    	 {
	    	        
	    	        
	            //SQL Code and vars
	    		String user = Log_In.getUser();
	    		String pw =  Log_In.getPassword().toString();
	    		
	    		//Load the Oracle JDBC driver
	    		DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
	    		
	    		//Connect to the database
	    		Connection conn = DriverManager.getConnection("jdbc:oracle:thin:hr/hr@oracle1.cise.ufl.edu:1521:orcl",
	    				user, pw);
//	    		float s = f;
	 			
	 			//View statements
	 			Statement viewStmt = conn.createStatement();
	 			String createView = "CREATE VIEW IFREQ AS " +
	 					"SELECT ITEMID, COUNT(TRANSID) AS ITEMCT " +
	 					"FROM TRANS " +
	 					"GROUP BY ITEMID " +
	 					"ORDER BY ITEMID";
	 			viewStmt.executeUpdate(createView);
	 			
	 			createView = "CREATE VIEW TRANSCT AS " +
	 					"SELECT COUNT(DISTINCT TRANSID) AS TOTAL FROM TRANS";
	 			viewStmt.executeUpdate(createView);
	 			
	 			createView = "CREATE VIEW SUPPORTSET AS " +
	 						"SELECT ITEMID, PCT " + 
	 						"FROM (" +
	 						"SELECT ITEMID, ITEMCT, TOTAL, (ITEMCT / TOTAL) * 100 AS PCT " +
	 						"FROM IFREQ, TRANSCT) " +
	 						"WHERE PCT >= " + s;
	 			viewStmt.executeUpdate(createView);
	 			
	 			// Execute query
	 			Statement stmt = conn.createStatement();
	 			String query = "SELECT ITEMNAME, PCT " +
	 							"FROM ITEMS, SUPPORTSET " +
	 							"WHERE ITEMS.ITEMID = SUPPORTSET.ITEMID " +
	 							"ORDER BY PCT DESC";
	 			
	 			ResultSet rset = stmt.executeQuery(query);
	 				
	 			// Fetch results
	 			int numRows = 0;
	 			while(rset.next()) {
	 				numRows++;
	 				text = (rset.getString("ITEMNAME") + "... support = " + rset.getFloat("PCT") + "%");
	 				textArea.append(text+newline);
	 			}
	 			
	 			text = (numRows + " rows selected");
	 			textArea.append(text + newline);
	 			
	 			String dropView = "DROP VIEW IFREQ";
	 			viewStmt.executeUpdate(dropView);
	 			
	 			dropView = "DROP VIEW TRANSCT";
	 			viewStmt.executeUpdate(dropView);
	 			
	 			dropView = "DROP VIEW SUPPORTSET";
	 			viewStmt.executeUpdate(dropView);
	    	 }
	    	 catch (SQLException e) 
	    	 {
					// TODO Auto-generated catch block
	    		 textArea.append("Error in SQL" + newline);
	    		 textArea.append(e.getMessage());
	
			}
	         //Manipulate data contained in text and pass to SQL
    	 }
    	 else if(taskNumber == 2)
    	 {
    		 try
    		 {
    			 
    		 }
    		 catch (SQLException e) 
	    	 {
				// TODO Auto-generated catch block
	    		 textArea.append("Error in SQL" + newline);
	    		 textArea.append(e.getMessage()); 
    		 }
    	 }
        //Display user query in textArea
        textArea.append(text + newline);
        textField.selectAll();
        
        //Make sure the new text is visible, even if there
        //was a selection in the text area.
        textArea.setCaretPosition(textArea.getDocument().getLength());
    }

}